#!/usr/bin/python
# coding: utf-8
import locale
from AccessControl import ClassSecurityInfo
from Products.Five import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.CMFCore.utils import getToolByName
from Acquisition import aq_inner
from zope.interface import implements
from feunb.skin.browser.interfaces import IFE
from Products.CMFPlone import utils
from zope.app.component.hooks import getSite
from DateTime import DateTime
import datetime
#TODO: Metodo deprecated
from Products.CMFPlone.CatalogTool import is_folderish
#TODO: Usar este
#context.restrictedTraverse('@@plone').isStructuralFolder()

class FEView(BrowserView):
    implements(IFE)
    """View geral para o produto"""

    def __init__(self, context, request):
        self.context = aq_inner(context)
        self.request = aq_inner(request) 
        self.catalog = getSite().portal_catalog
        self.path = self.context.portal_url.getPortalPath()
        
        
        
    # Calendario de Eventos                  
    def getEvents(self, start, end):

        from Products.CMFPlone import Batch
        from DateTime import DateTime

        results = self.catalog.searchResults(portal_type='Event',
                                             start = { "query": [start, end+1], "range": "minmax" },
                                             sort_on='start',
                                             review_state='published')
                               
        return results

    def getDateNowDmy(self):
        """Retorna a data atual"""
        
        from DateTime import DateTime
        date = DateTime().strftime('%d/%m/%Y')
        return date


    def getDateNowYmd(self):
        """Retorna a data atual"""
        
        from DateTime import DateTime
        date = DateTime().strftime('%Y/%m/%d')
        return date        



    def getNomeMes(self):
        """Retorna o nome mes atual"""
        
        from DateTime import DateTime
        import datetime
        from dateutil.relativedelta import relativedelta
        
        dia = datetime.date.today().day
        mes = datetime.date.today().month
        ano = datetime.date.today().year

        dt1 = datetime.date(ano, mes, 01)
        dt2 = dt1 + relativedelta(months=+1)
        dt3 = dt1 + relativedelta(months=+2)        
        dt4 = dt1 + relativedelta(months=+3)  
        dt5 = dt1 + relativedelta(months=+4) 
        dt6 = dt1 + relativedelta(months=+5) 
                        
        data1_ini = DateTime(dt1.year, dt1.month, 01)
        data2_ini = DateTime(dt2.year, dt2.month, 01)
        data3_ini = DateTime(dt3.year, dt3.month, 01)
        data4_ini = DateTime(dt4.year, dt4.month, 01)
        data5_ini = DateTime(dt5.year, dt5.month, 01)        
        data6_ini = DateTime(dt6.year, dt6.month, 01)         
                
        dtf1 = dt1 + relativedelta(months=+1) + relativedelta(days=-1)   
        dtf2 = dt2 + relativedelta(months=+1) + relativedelta(days=-1)
        dtf3 = dt3 + relativedelta(months=+1) + relativedelta(days=-1)
        dtf4 = dt4 + relativedelta(months=+1) + relativedelta(days=-1)
        dtf5 = dt5 + relativedelta(months=+1) + relativedelta(days=-1)
        dtf6 = dt6 + relativedelta(months=+1) + relativedelta(days=-1)
                        
        data1_fim = DateTime(dtf1.year, dtf1.month, dtf1.day, 23, 59, 59)
        data2_fim = DateTime(dtf2.year, dtf2.month, dtf2.day, 23, 59, 59)
        data3_fim = DateTime(dtf3.year, dtf3.month, dtf3.day, 23, 59, 59)               
        data4_fim = DateTime(dtf4.year, dtf4.month, dtf4.day, 23, 59, 59) 
        data5_fim = DateTime(dtf5.year, dtf5.month, dtf5.day, 23, 59, 59)
        data6_fim = DateTime(dtf6.year, dtf6.month, dtf6.day, 23, 59, 59)
                                
        data1b = data1_ini.strftime('%B')
        data2b = data2_ini.strftime('%B')
        data3b = data3_ini.strftime('%B')
        data4b = data4_ini.strftime('%B')
        data5b = data5_ini.strftime('%B')
        data6b = data6_ini.strftime('%B')
                
        data1f = self.getNomeMesPtbr(data1b)
        data2f = self.getNomeMesPtbr(data2b)
        data3f = self.getNomeMesPtbr(data3b)
        data4f = self.getNomeMesPtbr(data4b)       
        data5f = self.getNomeMesPtbr(data5b)
        data6f = self.getNomeMesPtbr(data6b)
                        
        datas = {
                 '01':{'mes':data1f,
                       'events':self.getEvents(data1_ini, data1_fim)
                      },

                 '02':{'mes':data2f,
                       'events':self.getEvents(data2_ini, data2_fim)
                      },
    
                 '03':{'mes':data3f,
                       'events':self.getEvents(data3_ini, data3_fim)
                      },
                      
                 '04':{'mes':data4f,
                       'events':self.getEvents(data4_ini, data4_fim)
                      },   
                      
                 '05':{'mes':data5f,
                       'events':self.getEvents(data5_ini, data5_fim)
                      },   
                      
                 '06':{'mes':data6f,
                       'events':self.getEvents(data6_ini, data6_fim)
                      },                                                                
                }

        return datas  


    def getNomeMesPtbr(self, mes_ingles):

        meses ={'January':'janeiro',
                'February':'fevereiro',
                'March':'março',
                'April':'abril',
                'May':'maio',
                'June':'junho',
                'July':'julho',
                'August':'agosto',
                'September':'setembro',
                'October':'outubro',
                'November':'novembro',
                'December':'dezembro'}

        formata = meses[mes_ingles][0].upper()+meses[mes_ingles][1:]
        return formata
